package cn.org.eshow.webapp.action;

import cn.org.eshow.bean.query.ProductOrderQuery;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.model.ProductOrder;
import cn.org.eshow.service.ProductOrderManager;
import cn.org.eshow.util.PageUtil;
import cn.org.eshow.webapp.util.RenderUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Results({@Result(name = "input", location = "add"),
        @Result(name = "list", type = "redirect", location = ""),
        @Result(name = "success", type = "redirect", location = "view/${id}"),
        @Result(name = "redirect", type = "redirect", location = "${redirect}")})
@AllowedMethods({"list", "search", "cancelOrder", "delivery", "conform", "view","exportExcel"})
public class ProductOrderAction extends BaseAction {

    private static final long serialVersionUID = 1L;

    @Autowired
    private ProductOrderManager productOrderManager;

    private List<ProductOrder> productOrders = new ArrayList<ProductOrder>();
    private ProductOrder productOrder = new ProductOrder();
    private ProductOrderQuery query = new ProductOrderQuery();

    /**
     * @return
     */
    public String list() {
        productOrders = productOrderManager.list(query);
        return LIST;
    }

    /**
     * @return
     */
    public String search() {
        Page<ProductOrder> page = productOrderManager.search(query);
        productOrders = page.getDataList();
        saveRequest("page", PageUtil.conversion(page));
        saveRequest("query", query);
        return LIST;
    }


    /**
     * @return
     */
    public String view() {
        if (id != null) {
            productOrder = productOrderManager.get(id);
            productOrder = productOrderManager.save(productOrder);
        } else {
            productOrder = new ProductOrder();
        }
        return REDIRECT;
    }

    /**
     * 取消订单
     *
     * @return
     * @throws Exception
     */
    public void cancelOrder() throws Exception {
        if (id == null) {
            RenderUtil.failure("缺少参数");
            return;
        }
        ProductOrder old = productOrderManager.get(id);
        if (old.getState() != 0 || old.getPayState() != 0) {
            RenderUtil.failure("接单失败,当前订单不处于待支付状态");
            return;
        }

        productOrderManager.cancel(old);
        saveMessage("取消成功");
        RenderUtil.success("取消成功");
        return;
    }


    /**
     * 接单
     *
     * @return
     * @throws Exception
     */
    public void delivery() throws Exception {
        if (id == null) {
            RenderUtil.failure("缺少参数");
            return;
        }
        ProductOrder old = productOrderManager.get(id);
        if (old.getState() != 1 || old.getDeliveryState() != 0) {
            RenderUtil.failure("接单失败,当前订单不处于待接单状态");
            return;
        }

        productOrderManager.delivery(old);
        saveMessage("成功接单");
        RenderUtil.success("成功接单");
        return;
    }

    /**
     * 确认收货
     *
     * @return
     * @throws Exception
     */
    public void conform() throws Exception {
        if (id == null) {
            RenderUtil.failure("缺少参数");
            return;
        }
        ProductOrder old = productOrderManager.get(id);
        if (old.getState() != 1 || old.getDeliveryState() != 1) {
            RenderUtil.failure("接单失败,当前订单不处于待配送状态");
            return;
        }

        productOrderManager.conform(old);
        saveMessage("确认收货");
        RenderUtil.success("确认收货");
        return;
    }

    /**
     * 导出Excel返回码
     */
    public void exportExcel() {

        List<ProductOrder> productOrders = productOrderManager.list(query);//不分页
        String url = productOrderManager.exportExcel(productOrders);
        if (StringUtils.isEmpty(url)) {
            RenderUtil.failure("导出失败，请稍后重试");
            return;
        }
        Map<String, String> map = new HashMap<String, String>();
        map.put("url", "\"" + url + "\"");
        RenderUtil.result(1, "导出成功", map);
    }


    public List<ProductOrder> getProductOrders() {
        return productOrders;
    }

    public void setProductOrders(List<ProductOrder> productOrders) {
        this.productOrders = productOrders;
    }

    public ProductOrder getProductOrder() {
        return productOrder;
    }

    public void setProductOrder(ProductOrder productOrder) {
        this.productOrder = productOrder;
    }

    public ProductOrderQuery getQuery() {
        return query;
    }

    public void setQuery(ProductOrderQuery query) {
        this.query = query;
    }

}